function coordinateIsValid(board, coordinate, color){
    let [x, y] = coordinate;
    return x >= 0 && x < board.length && y >= 0 && y < board.length && board[y][x] === color;
}
function createIsWin(p1Move, p2Move){
    return function (board, coordinate, color){
        let p1 = p1Move(coordinate);
        let p2 = p2Move(coordinate);
        let count = 1;
        while(1){
            let p1Flag = false;
            if(coordinateIsValid(board, p1, color)){
                p1Flag = true;
                count++;
                p1 = p1Move(p1);
            }
            let p2Flag = false;
            if(coordinateIsValid(board, p2, color)){
                p2Flag = true;
                count++;
                p2 = p2Move(p2);
            }
            if(!p1Flag && !p2Flag){
                return false;
            }
            if(count >= 5){
                return true;
            }
        }
        return false;
    }
}


let checkHorizontal = createIsWin(
    ([x, y]) => [x - 1, y],
    ([x, y]) => [x + 1, y]
);
let checkVertical = createIsWin(
    ([x, y]) => [x, y + 1],
    ([x, y]) => [x, y - 1]
);
let checkDiagonal = createIsWin(
    ([x, y]) => [x + 1, y - 1],
    ([x, y]) => [x - 1, y + 1]
);
let checkReDiagonal = createIsWin(
    ([x, y]) => [x + 1, y + 1],
    ([x, y]) => [x - 1, y - 1]
);
export function checkGameOver(board, coordinate, color){
    return checkHorizontal(board, coordinate, color) || checkVertical(board, coordinate, color) || checkDiagonal(board, coordinate, color) || checkReDiagonal(board, coordinate, color);
}